library(foreign)
library(lme4)
library(Zelig)
library(ZeligMultilevel)
library(texreg)

######################################################################
###Data on party mergers
data<-read.csv(file="datamergers.csv")
######################################################################

######################################################################
####Number of mergers, constituent parties############################
##############################NUMBER OF MERGERS (Figure 1)############
pdf("nomergers.pdf")
r<-data
r<-subset(r,r$merger==1)
r<-subset(r,!(duplicated(r$party)=="TRUE"))
par(mar=c(7,5,2,2))
par(mfrow=c(1,1))
par(las=2)
par(cex.axis=1.5)
par(mar=c(8, 4, 3, 2) )
bardat<-table(r$merger,r$country)
barplot(bardat,cex.names=0.7,cex.axis=1,
ylim=c(0,10))
dev.off()
######################################################################

##############################NUMBER OF CONSTITUENT PARTIES###########
###Parties nested in elections
###Create a dataset of parties nested in elections from the dataset with party dyads nested in elections
e<-subset(data,data$merger==1)
e1<-e[c("country","el_year","party1","party")]
e2<-e[c("country","el_year","party2","party")]
names(e1)<-c("country","el_year","party","mergedparty")
names(e2)<-c("country","el_year","party","mergedparty")
e<-rbind(e1,e2)
e<-subset(e,duplicated(paste(e$country,e$el_year,e$party))=="FALSE")
length(table(e$mergedparty))
length(table(e$party))##Number of constituent parties
length(table(paste(e$country,e$el_year)))##Number of electoral periods with mergers
sort(table(e$mergedparty))
######################################################################

######################################################################
##Exclude two electoral periods when "mega-mergers" took place
##(Spain 1977-1979 and Bulgaria 1994-1997)
data1<-subset(data,data$el_index!=43&data$el_index!=259)
######################################################################

######################################################################
####MAIN MODELS (Table 2)#############################################
ri1<-glmer(merger~closeth+closelarg30+ideology1+PEC+prevgov+
meanlogage+(1|el_index)+(1|country),family=binomial(link="logit"),
data=data1,control = glmerControl(optimizer = "bobyqa"))
summary(ri1)

ri2<-glmer(merger~closeth+closelarg40+ideology1+PEC+prevgov+
meanlogage+(1|el_index)+(1|country),family=binomial(link="logit"),
data=data1,control = glmerControl(optimizer = "bobyqa"))
summary(ri2)

ri3<-glmer(merger~closeth+closelarg50+ideology1+PEC+prevgov+
meanlogage+(1|el_index)+(1|country),family=binomial(link="logit"),
data=data1,control = glmerControl(optimizer = "bobyqa"))
summary(ri3)

ri4<-glmer(merger~closeth+closelarg30+closelarg40+closelarg50+ideology1+PEC+prevgov+
meanlogage+(1|el_index)+(1|country),family=binomial(link="logit"),
data=data1,control = glmerControl(optimizer = "bobyqa"))
summary(ri4)

ri5<-glmer(merger~closeth+ablarg+ideology1+PEC+prevgov+meanlogage+
(1|el_index)+(1|country),family=binomial(link="logit"),data=data1,
control = glmerControl(optimizer = "bobyqa"))
summary(ri5)

ri6<-glmer(merger~closeth+ablarg+closelarg30+closelarg40+closelarg50+ideology1+PEC+prevgov+
meanlogage+(1|el_index)+(1|country),family=binomial(link="logit"),
data=data1,control = glmerControl(optimizer = "bobyqa"))
summary(ri6)

results<-texreg(list(ri1,ri2,ri3,ri4,ri5,ri6),stars = c(0.001,
0.01, 0.05),
custom.coef.names=
c("Intercept","Close to electoral threshold","Close to 30%","Ideological similarity",
"Electoral coalition","Government coalition","Mean (ln(party age))","Close to 40%",
"Close to 50%","Close to the largest party"),
include.pvalues = TRUE,naive=TRUE)
######################################################################

######################################################################
#Prediction
summary(attributes(ri6)$resp$mu[data1$merger==1])
summary(attributes(ri6)$resp$mu[data1$merger==0])
######################################################################

######################################################################
###Substantive effect of predictor variables on merger
data1$country<-as.character(data1$country)
modelz<-zelig(merger~closeth+closelarg30+ideology1+PEC+prevgov+
meanlogage+tag(1|el_index)+tag(1|country),model="logit.mixed",
data=data1,control = glmerControl(optimizer = "bobyqa"))
summary(modelz)

#Closeness to the electoral threshold
x<-setx(modelz,closeth=mean(data1$closeth)-sd(data1$closeth),PEC=0,prevgov=0,ideology1=0,ablarg=mean(data1$ablarg),
closelarg30=mean(data1$closelarg30),closemajvote=mean(data1$closemajvote))
x1<-setx(modelz,closeth=mean(data1$closeth),PEC=0,prevgov=0,ideology1=0,ablarg=mean(data1$ablarg),
closelarg30=mean(data1$closelarg30),closemajvote=mean(data1$closemajvote))
s.out1<-sim(modelz,x=x,x1=x1)

#Closeness to the half the winning threshold
x<-setx(modelz,closelarg30=mean(data1$closelarg30)-sd(data1$closelarg30),PEC=0,closeth=mean(data1$closeth),
ablarg=mean(data1$ablarg),closemajvote=mean(data1$closemajvote),
prevgov=0,ideology1=0)
x1<-setx(modelz,closelarg30=mean(data1$closelarg30),PEC=0,closeth=mean(data1$closeth),
ablarg=mean(data1$ablarg),closemajvote=mean(data1$closemajvote),prevgov=0,ideology1=0)
s.out2<-sim(modelz,x=x,x1=x1)

#Ideological similarity
x<-setx(modelz,PEC=0,prevgov=0,ideology1=0,PEC=0,ablarg=mean(data1$ablarg),
closelarg30=mean(data1$closelarg30),closemajvote=mean(data1$closemajvote))
x1<-setx(modelz,PEC=0,prevgov=0,ideology1=1,PEC=0,ablarg=mean(data1$ablarg),
closelarg30=mean(data1$closelarg30),closemajvote=mean(data1$closemajvote))
s.out3<-sim(modelz,x=x,x1=x1)

#Pre-electoral coalition
x<-setx(modelz,prevgov=0,ideology1=0,PEC=0,closeth=mean(data1$closeth),ablarg=mean(data1$ablarg),
closelarg30=mean(data1$closelarg30),closemajvote=mean(data1$closemajvote))
x1<-setx(modelz,prevgov=0,ideology1=0,PEC=1,closeth=mean(data1$closeth),ablarg=mean(data1$ablarg),
closelarg30=mean(data1$closelarg30),closemajvote=mean(data1$closemajvote))
s.out4<-sim(modelz,x=x,x1=x1)

#Government
x<-setx(modelz,PEC=0,prevgov=0,ideology1=0,PEC=0,closeth=mean(data1$closeth),ablarg=mean(data1$ablarg),
closelarg30=mean(data1$closelarg30),closemajvote=mean(data1$closemajvote))
x1<-setx(modelz,PEC=0,prevgov=1,ideology1=0,PEC=0,closeth=mean(data1$closeth),ablarg=mean(data1$ablarg),
closelarg30=mean(data1$closelarg30),closemajvote=mean(data1$closemajvote))
s.out5<-sim(modelz,x=x,x1=x1)

#Party age
x<-setx(modelz,meanlogage=mean(data1$meanlogage),PEC=0,
closeth=mean(data1$closeth),prevgov=0,
ideology1=0,PEC=0,ablarg=mean(data1$ablarg),
closelarg30=mean(data1$closelarg30),closemajvote=mean(data1$closemajvote))
x1<-setx(modelz,meanlogage=mean(data1$meanlogage)+sd(data1$meanlogage),PEC=0,prevgov=0,ideology1=0,PEC=0,
closeth=mean(data1$closeth),ablarg=mean(data1$ablarg),
closelarg30=mean(data1$closelarg30),closemajvote=mean(data1$closemajvote))
s.out6<-sim(modelz,x=x,x1=x1)
######################################################################

######################################################################
######################################################################
####VALIDITY OF IDEOLOGICAL SIMILARITY MEASURE#####################
##############1.COMPARE THE PARTY FAMILY MEASURE WITH CMP MEASURE############################
####Absolute distance in CMP rile scores as an alternative measure of ideological similarity
data1$rile<-abs(data1$rile1-data1$rile2)
#How many merger dyads are NOT covered by CMP?
length(data1$merger[is.na(data1$rile)=="TRUE"&data1$merger==1])
##For how many of them the estimated difference of left-right positions is equal to 0?
length(data1$merger[data1$rile==0&data1$merger==1&is.na(data1$rile)=="FALSE"])
##Correlation between party family measures and CMP measures
datacmp<-subset(data1,is.na(data1$rile)=="FALSE")
cor(datacmp$rile,datacmp$ideology1,method="spearman")

##############2.COMPARE THE PARTY FAMILY MEASURE WITH CHES MEASURE############################
####Absolute distance in CHES general left-right scores as an alternative measure of ideological similarity
data1$lrgen<-abs(data1$lrgen1-data1$lrgen2)
##How many merger dyads are covered by CHES?
length(data1$merger[is.na(data1$lrgen)=="FALSE"&data1$merger==1])
##How many party dyads are covered by CHES?
length(data1$el_index[is.na(data1$lrgen)=="FALSE"])
##How many electoral periods are covered by CHES?
length(table(data1$el_index[is.na(data1$lrgen)=="FALSE"]))
##Correlation between party family measures and CHES measures
dataches<-subset(data1,is.na(data1$lrgen)=="FALSE")
cor(dataches$lrgen,dataches$ideology1,method="spearman")

######################################################################
##3.How many party-election dyads lack parlgov or bugajski measures?
###Parties nested in elections
###Create a dataset of parties nested in elections from the dataset with party dyads nested in elections
e1<-data1[c("country","el_year","party1","family1","family_bugajski1","family_parlgov1")]
e2<-data1[c("country","el_year","party2","family2","family_bugajski2","family_parlgov2")]
names(e1)<-c("country","el_year","party","family","family_bugajski","family_parlgov")
names(e2)<-c("country","el_year","party","family","family_bugajski","family_parlgov")
e<-rbind(e1,e2)
e<-subset(e,duplicated(paste(e$country,e$el_year,e$party))=="FALSE")
e$misfam<-0
##Party-election dyads without codes from Parlgov or Bugajski
e$misfam[is.na(e$family_parlgov)=="TRUE"&is.na(e$family_bugajski)=="TRUE"]<-1
##Party-election dyads with different codes than those provided by Parlgov or Bugajski
e$misfam[e$family!=e$family_parlgov&e$family!=e$family_bugajski&is.na(e$family_bugajski)=="FALSE"&
is.na(e$family_parlgov)=="FALSE"]<-1
e$misfam[e$family!=e$family_parlgov&is.na(e$family_bugajski)=="TRUE"&
is.na(e$family_parlgov)=="FALSE"]<-1
e$misfam[e$family!=e$family_bugajski&is.na(e$family_bugajski)=="FALSE"&
is.na(e$family_parlgov)=="TRUE"]<-1
table(e$misfam)
100-((table(e$misfam)[2])/length(e$misfam)*100)

##Party dyads nested in elections
data1$misfam<-0
##Party-dyads nested in elections without codes from Parlgov or Bugajski
data1$misfam[(is.na(data1$family_parlgov1)=="TRUE"&is.na(data1$family_bugajski1)=="TRUE")|
(is.na(data1$family_parlgov2)=="TRUE"&is.na(data1$family_bugajski2)=="TRUE")]<-1
##Party-dyads nested in elections with different codes than those provided by Parlgov or Bugajski
data1$misfam1<-0
data1$misfam1[data1$family1!=data1$family_parlgov1&data1$family1!=data1$family_bugajski1&is.na(data1$family_bugajski1)=="FALSE"&
is.na(data1$family_parlgov1)=="FALSE"]<-1
data1$misfam1[data1$family1!=data1$family_parlgov1&is.na(data1$family_bugajski1)=="TRUE"&
is.na(data1$family_parlgov1)=="FALSE"]<-1
data1$misfam1[data1$family1!=data1$family_bugajski1&is.na(data1$family_bugajski1)=="FALSE"&
is.na(data1$family_parlgov1)=="TRUE"]<-1
data1$misfam2<-0
data1$misfam2[data1$family2!=data1$family_parlgov2&data1$family2!=data1$family_bugajski2&is.na(data1$family_bugajski2)=="FALSE"&
is.na(data1$family_parlgov2)=="FALSE"]<-1
data1$misfam2[data1$family2!=data1$family_parlgov2&is.na(data1$family_bugajski2)=="TRUE"&
is.na(data1$family_parlgov2)=="FALSE"]<-1
data1$misfam2[data1$family2!=data1$family_bugajski2&is.na(data1$family_bugajski2)=="FALSE"&
is.na(data1$family_parlgov2)=="TRUE"]<-1
data1$misfam[data1$misfam1==1|data1$misfam2==1]<-1
table(data1$misfam)
100-((table(data1$misfam)[2])/length(data1$misfam)*100)

####4.Additional analysis using only the cases for which parlgov or Bugajski measures were available
ri1parlgovbug<-update(ri1,subset=misfam==0)
ri2parlgovbug<-update(ri2,subset=misfam==0)
ri3parlgovbug<-update(ri3,subset=misfam==0)
ri4parlgovbug<-update(ri4,subset=misfam==0)
ri5parlgovbug<-update(ri5,subset=misfam==0)
ri6parlgovbug<-update(ri6,subset=misfam==0)
results<-texreg(list(ri1parlgovbug,ri2parlgovbug,ri3parlgovbug,
ri4parlgovbug,ri5parlgovbug,ri6parlgovbug),stars = c(0.001,
0.01, 0.05),
custom.coef.names=
c("Intercept","Close to electoral threshold","Close to 30%","Ideological similarity",
"Electoral coalition","Government coalition","Mean (ln(party age))","Close to 40%",
"Close to 50%","Close to the largest party"),
include.pvalues = TRUE,naive=TRUE)
######################################################################
######################################################################
######################################################################

######################################################################
######################################################################
####Descriptive statistics (Table 3)#####################
desc<-data1[c("merger","closeth","closelarg30","closelarg40","closelarg50",
"ablarg","ideology1","PEC","prevgov","meanlogage")]
summary(desc)
sd(desc$closeth)
sd(desc$closelarg30)
sd(desc$closelarg40)
sd(desc$closelarg50)
sd(desc$ablarg)
sd(desc$meanlogage)
######################################################################
######################################################################


######################################################################
######################################################################
####ROBUSTNESS CHECKS#################################################

###Western Europe (Table 4)
ri1w<-update(ri1,subset=new==0)
ri2w<-update(ri2,subset=new==0)
ri3w<-update(ri3,subset=new==0)
ri4w<-update(ri4,subset=new==0)
ri5w<-update(ri5,subset=new==0)
ri6w<-update(ri6,subset=new==0)

resultswe<-texreg(list(ri1w,ri2w,ri3w,ri4w,ri5w,ri6w),stars = c(0.001,
0.01, 0.05),
custom.coef.names=
c("Intercept","Close to electoral threshold","Close to 30%","Ideological similarity",
"Electoral coalition","Government coalition","Mean (ln(party age))","Close to 40%",
"Close to 50%","Close to the largest party"),
include.pvalues = TRUE,naive=TRUE)

###Eastern Europe (Table 5)
ri1e<-update(ri1,subset=new==1)
ri2e<-update(ri2,subset=new==1)
ri3e<-update(ri3,subset=new==1)
ri4e<-update(ri4,subset=new==1)
ri5e<-update(ri5,subset=new==1)
ri6e<-update(ri6,subset=new==1)

resultsee<-texreg(list(ri1e,ri2e,ri3e,ri4e,ri5e,ri6e),stars = c(0.001,
0.01, 0.05),
custom.coef.names=
c("Intercept","Close to electoral threshold","Close to 30%","Ideological similarity",
"Electoral coalition","Government coalition","Mean (ln(party age))","Close to 40%",
"Close to 50%","Close to the largest party"),
include.pvalues = TRUE,naive=TRUE)

###The ideology variable as a categorical variable (Table 6)
ri1i<-update(ri1,~.-ideology1+as.factor(ideology1))
ri2i<-update(ri2,~.-ideology1+as.factor(ideology1))
ri3i<-update(ri3,~.-ideology1+as.factor(ideology1))
ri4i<-update(ri4,~.-ideology1+as.factor(ideology1))
ri5i<-update(ri5,~.-ideology1+as.factor(ideology1))
ri6i<-update(ri6,~.-ideology1+as.factor(ideology1))

resultsideology<-texreg(list(ri1i,ri2i,ri3i,ri4i,ri5i,ri6i),stars = c(0.001,
0.01, 0.05),
custom.coef.names=
c("Intercept","Close to electoral threshold","Close to 30%","Similar family","Same family",
"Electoral coalition","Government coalition","Mean (ln(party age))","Close to 40%",
"Close to 50%","Close to the largest party"),
include.pvalues = TRUE,naive=TRUE)

######################Institutional variables (Table 7)
riinst1<-update(ri6,~.+investiture+bical+regime)
riinst2<-update(ri1,~.+investiture*closelarg30+bical+regime)
riinst3<-update(ri1,~.+investiture+bical+regime*closelarg30)
riinst4<-update(ri2,~.+investiture*closelarg40+bical+regime)
riinst5<-update(ri2,~.+investiture+bical+regime*closelarg40)
riinst6<-update(ri3,~.+investiture*closelarg50+bical+regime)
riinst7<-update(ri3,~.+investiture+bical+regime*closelarg50)

resultsinst<-texreg(list(riinst1,riinst2,riinst3,riinst4,riinst5,riinst6,riinst7),stars = c(0.001,
0.01, 0.05),
custom.coef.names=
c("Intercept","Close to electoral threshold","Close to the largest party","Close to 30%","Close to 40%",
"Close to 50%","Ideological similarity",
"Electoral coalition","Government coalition","Mean (ln(party age))",
"Investiture vote","Bicameralism","Semi-presidentialism",
"Investiture * Close to 30%","Semi-pres. * Close to 30%",
"Investiture * Close to 40%","Semi-pres. * Close to 40%",
"Investiture * Close to 50%","Semi-pres. * Close to 50%"),
include.pvalues = TRUE,naive=TRUE)

######################################################################
######################################################################
